package com.ipharma.application.services.transaccion;

import java.sql.Timestamp;
import java.util.Iterator;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.config.Config;
import com.ipharma.config.FormateadorDeFechas;
import com.ipharma.config.Session;
import com.ipharma.dto.Product;
import com.ipharma.model.Transaccion;

/**
 * A TransactionExtract class.
 * <P>
 * 
 * @author JorgE.H
 */
public class TransactionExtract extends Transaction {

   /**
    * Attributes
    */
   // AlertsPlus dbAlert = new AlertsPlus();
   /**
    * Constructor
    */
   public TransactionExtract(Session pSess) {

      super(pSess);
      type = EXTRACT;
   }

   /**
    * Checks transaction for alerts
    * 
    * @param pProdId  product id
    * @param pUserId user id
    * @param pDispId dispenser id
    * @return and id alert or -1 if there is no alerts
    */
   public int check(int pProdId, int pUserId, int pDispId) throws Exception {

      return dbTrans.checkAlerta(new Long(pProdId), new Long(pUserId), new Long(pDispId)).intValue();
      //return dbAlert.check(theSess.getConn(), pProdId, pUserId, pDispId);
   }

   /**
    * Ends the trasaction, optionally prints a ticket
    * 
    * @param pAlert
    *           an alert code
    * @return true if succedded, false otherwise
    */
   public boolean end(int pAlert) {

      boolean lb_alerts = false;
      try {
         Iterator l_ite = (Iterator) items.elements();
         while (l_ite.hasNext()) {
            // prepares data
            Product l_prod = (Product) l_ite.next();
            int li_pdtId = l_prod.getId();
            int li_qty = l_prod.getQuantity();
            Config config = (Config) theSess.get(McdPrs.CONFIG);
            int li_dprId = Integer.parseInt(config.getDispenser());
            int li_type = EXTRACT;
            int li_usrId = theSess.getUsuario().getId().intValue();
            String ls_status = STATUS_OK;
            String ls_date = FormateadorDeFechas.getDateFormater().format(date.getTime());

            // checks alerts for extraction
            int li_alert = (pAlert < 0) ? pAlert : dbTrans.findAlertaByCode(McdPrs.alerts[pAlert]).getId().intValue();
            if (li_alert < 0)
               li_alert = check(li_pdtId, li_usrId, li_dprId);
            if (li_alert >= 0)
               lb_alerts = true;
            Integer l_alert = (li_alert < 0) ? null : new Integer(li_alert);

            // inserts transaction into database
            Transaccion transaccion = new Transaccion();
            transaccion.setFecha(new Timestamp(date.getTime().getTime()));
            transaccion.setCantidad(new Integer( li_qty ));
            transaccion.setEstado(ls_status);
            Long assignOpt = null;
            if (assignOptId != null)
               assignOpt = new Long(assignOptId.intValue());
            Long assign = null;
            if (assignId != null)
               assign = new Long(assignId.intValue());
            Long patient = null;
            if (patientId != null)
               patient = new Long(patientId.intValue());
            Long alert = null;
            if (l_alert != null)
               alert = new Long(l_alert.intValue());
            dbTrans.insertTransaction(transaccion, alert, assignOpt,
                                      assign, patient, new Long(li_type), 
                                      new Long(li_usrId), new Long(li_dprId), 
                                      new Long(li_pdtId));
            
            // optionally prints a ticket
            if (config.getTicketGeneration().equals("S"))
               // prints a ticket
               printTicket(l_prod);
         }
         // removes items from list
         items.clear();
      } catch (Exception e) {
         McdError.add(e);
      }
      return lb_alerts;
   }
}
